export default {
    data() {
        return {
            data: this.generateData(),
            customerList: [], //客服列表
            seller_id: 0,
            // 表单数据
            ruleForm: {
                supplier_name: '', //供应商名称
                leading_official_name: '', //负责人姓名
                leading_official_phone: '', //负责人电话
                login_num: '', //登陆账号
                login_password: '', //登陆密码
                // custom_service_num: '',                  //客服数量
                auth: [], //权限
                status: '1',
                customer_id: '', //客服id
            },

            // 表单验证
            rules: {
                supplier_name: [{
                    required: true,
                    message: '请输入供应商名称',
                    trigger: 'blur'
                }, ],
                leading_official_name: [{
                    required: true,
                    message: '请输入负责人姓名',
                    trigger: 'blur'
                }, ],
                customer_id: [{
                    required: true,
                    message: '请选择客服人员',
                    trigger: 'blur'
                }, ],
                leading_official_phone: [{
                    required: true,
                    message: '请输入负责人电话',
                    trigger: 'blur'
                }, ],
                login_num: [{
                    required: true,
                    message: '请输入登陆账号',
                    trigger: 'blur'
                }, ],
                login_password: [{
                    required: true,
                    message: '请输入登陆密码',
                    change: 'blur'
                }, ],
                // custom_service_num: [{
                //     required: true,
                //     message: '请输入客服数量',
                //     change: 'blur'
                // },],
                status: [{
                    required: true,
                    message: '请选择状态',
                    change: 'blur'
                }, ]
            },
        }
    },

    //进入页面加载
    mounted: function () {
        var that = this;
        //在缓存中获取值
        that.user_name = sessionStorage.getItem("user_name");
        that.user_id = sessionStorage.getItem("user_id");
        that.token = sessionStorage.getItem("access-token");
        if ((!that.token) || (!that.user_id) || (!that.user_name)) {
            that.$router.push('/login');
        }

        // 获取单个员工数据
        if (that.$route.query.seller_id) {
            that.seller_id = that.$route.query.seller_id;
            that.detail();
        }
        that.getCustomerList();
    },

    methods: {

        /**
         * 权限列表
         */
        generateData() {
            const data = [];
            const data2 = [{
                id: 24,
                name: '闪购'
            }, {
                id: 25,
                name: '非趣不可'
            }, {
                id: 27,
                name: '游轮趣玩'
            }];
            for (let i = 0; i < data2.length; i++) {
                data.push({
                    key: i,
                    id: data2[i].id,
                    label: data2[i].name,
                    disabled: false
                });
            }
            return data;
        },

        /**
         * 获取单个员工数据
         */
        detail() {
            const that = this;

            //请求的数据
            const formData = {};
            formData.token = that.token;
            formData.seller_id = that.seller_id;

            //请求邀请者列表
            that.$http.post(that.adminApi.api_url + "/Operator/editshow", formData, {
                emulateJSON: true
            }).then(
                function (res) {
                    // 处理成功的结果
                    that.ruleForm.supplier_name = res.body.company;
                    that.ruleForm.leading_official_name = res.body.name;
                    that.ruleForm.leading_official_phone = res.body.mobile;
                    that.ruleForm.login_num = res.body.username;
                    that.ruleForm.customer_id = res.body.staffId;
                    that.ruleForm.status = res.body.status.toString();

                    //处理权限
                    if ((res.body.authority.length !== 0) && (that.data.length !== 0)) {
                        for (var i = 0; i < that.data.length; i++) {
                            for (var j = 0; j < res.body.authority.length; j++) {
                                if (parseInt(that.data[i].id) === parseInt(res.body.authority[j])) {
                                    that.ruleForm.auth.push(i);
                                }
                            }
                        }
                    }
                },
                function () {
                    // 处理失败的结果
                    that.$message({
                        type: 'error',
                        message: `操作提示: ${ '处理异常' }`
                    });
                });
        },

        /**
         * 保存员工数据
         */
        save: function (formName) {

            let that = this;
            this.$refs[formName].validate((valid) => {
                if (valid) {
                    if (that.seller_id === 0) {
                        that.add();
                    } else {
                        that.edit();
                    }
                } else {
                    // console.log('error submit!!');
                    return false;
                }
            });
        },

        /**
         * 添加
         */
        add: function () {
            const that = this;
            if (that.ruleForm.auth.length === 0) {
                that.$message({
                    type: 'error',
                    message: `操作提示: ${ '请分配权限' }`
                });
                return false;
            }
            //请求的数据
            const formData = {};
            formData.token = that.token;
            formData.name = that.ruleForm.leading_official_name; //负责人姓名
            formData.company = that.ruleForm.supplier_name; //供应商名称
            formData.mobile = that.ruleForm.leading_official_phone; //负责人电话
            formData.username = that.ruleForm.login_num; //登录账号
            formData.password = that.ruleForm.login_password; // 登录密码
            formData.staffId = that.ruleForm.customer_id;
            //处理id
            const menu_id = [];
            for (var i in that.ruleForm.auth) {
                const index = that.ruleForm.auth[i];
                menu_id.push(that.data[index].id);
            }

            formData.auth_id = menu_id; //权限ID
            formData.status = that.ruleForm.status; //1,开启。2关闭

            that.$http.post(that.adminApi.api_url + "/Operator/operatoradd", formData, {
                emulateJSON: true
            }).then(
                function (res) {
                    var data = res.body;
                    // 处理成功的结果
                    that.$message.success('添加成功');
                    that.$router.back(-1);
                },
                function () {
                    // 处理失败的结果
                    that.$message({
                        type: 'error',
                        message: `操作提示: ${ '处理异常' }`
                    });
                });
        },

        /**
         * 修改
         */
        edit: function () {
            const that = this;
            if (that.ruleForm.auth.length === 0) {
                that.$message({
                    type: 'error',
                    message: `操作提示: ${ '请分配权限' }`
                });
                return false;
            }
            //请求的数据
            const formData = {};
            formData.token = that.token;
            formData.name = that.ruleForm.leading_official_name; //负责人姓名
            formData.company = that.ruleForm.supplier_name; //供应商名称
            formData.mobile = that.ruleForm.leading_official_phone; //负责人电话
            formData.username = that.ruleForm.login_num; //登录账号
            formData.password = that.ruleForm.login_password; // 登录密码
            formData.staffId = that.ruleForm.customer_id;
            formData.seller_id = that.seller_id;
            //处理id
            const menu_id = [];
            for (var i in that.ruleForm.auth) {
                const index = that.ruleForm.auth[i];
                menu_id.push(that.data[index].id);
            }

            formData.auth_id = menu_id; //权限ID
            formData.status = that.ruleForm.status; //1,开启。2关闭

            that.$http.post(that.adminApi.api_url + "/Operator/operatoredit", formData, {
                emulateJSON: true
            }).then(
                function (res) {
                    // 处理成功的结果
                    that.$message.success('修改成功');
                    that.$router.back(-1);
                },
                function () {
                    // 处理失败的结果
                    that.$message({
                        type: 'error',
                        message: `操作提示: ${ '处理异常' }`
                    });
                });
        },

        /**
         * 获取客服列表
         */
        getCustomerList: function () {
            //请求的数据
            const that = this;
            const formData = {};
            formData.token = that.token;
            that.$http.post(that.adminApi.api_url + "/Operator/customerlist", formData, {
                emulateJSON: true
            }).then(
                function (res) {
                    if (res.body) {
                        for (var i in res.body) {
                            that.customerList.push({
                                staffId: res.body[i].staffId,
                                userName: res.body[i].userName,
                            });
                        }
                    }
                },
                function () {
                    // 处理失败的结果
                    that.$message({
                        type: 'error',
                        message: `操作提示: ${ '处理异常' }`
                    });
                });
        }

    }
}